<template>
  <div class="basic_info">
    <div>
      <div class="info_operation_btn">
        <p>消息</p>
        <div>
          <!-- <div>消息状态</div>
          <el-select placeholder="" v-model="queryData.xxzt">
            <el-option
              style="font-size: 12px"
              v-for="item in xxztOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>-->
          <input v-model="queryData.keywords" placeholder="输入关键字查询" />
          <!-- <el-input v-model="queryData.searchValue" placeholder="输入关键字查询"></el-input> -->
          <el-button type="primary" @click="onQuery">搜索</el-button>
        </div>
      </div>
      <el-empty description="暂无数据" v-if="total === 0"></el-empty>
      <div class="info_content_div" v-else>
        <div @click="toDetails(item)" v-for="(item, index) in listData" :key="index">
          <div>
            <p>{{ item.titile }}</p>
            <div class="div_abstract">{{ item.msgAbstract }}</div>
          </div>
          <div class="label_div">
            <el-button v-if="isLeader === 1" type="primary" @click.stop="clickBuy">续费</el-button>
          </div>
          <div>
            <p></p>
            <span>{{ item.state }}</span>
          </div>
          <div>
            <p></p>
            <div class="div_time">
              <span>{{ formatTime(item) }}</span>
            </div>
          </div>
        </div>
      </div>
      <div class="table_pagination">
        <div class="pagination_div">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryData.pageNo"
            :page-sizes="[20, 50, 100, 200]"
            :page-size="queryData.pageSize"
            layout="jumper, prev, pager,next, sizes"
            :total="total"
          ></el-pagination>
        </div>
      </div>
    </div>
    <!-- 消息详情页面 -->
    <DialogNotice ref="dialogNotice"></DialogNotice>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getAction } from '@/api/manage'
import { formatTime } from '@/utils/index'
// 消息详情页面
import DialogNotice from '../components/dialogNotice'
import Vue from 'vue'
import { USER_INFO, ACCOUNT_BOOK } from '@/store/mutation-types'

export default {
  name: 'newsNotice',
  components: {
    DialogNotice,
  },
  computed: {
    ...mapGetters(['name']),
  },
  created () {
    // 获取是否是leader
    this.getIsLeader()
    this.getNotice()
  },
  data () {
    return {
      listData: [],
      // 退货仓库
      xxztOptions: [
        {
          value: '全部',
          label: '全部',
        },
        {
          value: '已读',
          label: '已读',
        },
        {
          value: '未读',
          label: '未读',
        },
      ],
      // 查询条件数据
      queryData: {
        // xxzt: '',
        keywords: '',
        msgCategory: '2', // 1:通知公告,产品动态 2:系统消息
        pageSize: 20, //每页的数据条数
        pageNo: 1, //默认开始页面
      },
      total: 0, // 总数
      isLeader: 0,
    }
  },
  methods: {
    //   搜索
    onQuery () {
      this.queryData.pageNo = 1
      this.getNotice()
    },
    // 获取消息列表
    getNotice () {
      let data = { ...this.queryData }
      getAction('/sys/annountCement/getAccountBeyondList', data).then((res) => {
        if (res.code === 200) {
          this.total = res.result.total
          let data = res.result.records || []
          data.forEach((item) => {
            item.readMsg = item.readFlag === '1' ? '已读' : '未读'
          })
          this.listData = data
        }
      })
    },
    // 获取是否是leader
    getIsLeader() {
      let userInfo = Vue.ls.get(USER_INFO)
      getAction('/accountbookusing/xghAccountBookUserUsing/getUserIsLeader', {
        userId: userInfo.id,
      }).then(res => {
        this.isLeader = res.result
      })
    },
    // 跳转至详情
    toDetails (item) {
      this.toRead(item.id)
      this.$refs.dialogNotice.show(item)
    },
    clickBuy() {
      // 暂未对接，页面跳转先注掉
      this.$elMessage.warning('该功能暂未开放！')
      // this.$router.push({
      //   path: '/setting/ManagementZT/ManagementZT',
      //   query: {
      //     editBook: true
      //   }
      // })
    },
    // 消息已读
    toRead (id) {
      getAction('/sys/annountCement/toReadAccountBeyond', { id }).then((res) => {
        if (res.code === 200) {
          this.$EventBus.$emit('READNOTICE', '')
        }
      })
    },
    // 获取每页几条数据
    handleSizeChange (val) {
      this.queryData.pageSize = val
      this.getNotice()
    },
    // 获取当前第几页
    handleCurrentChange (val) {
      this.queryData.pageNo = val
      this.getNotice()
    },
    formatTime (val) {
      if (!val.sendTime) {
        return val.sendTime
      }
      return formatTime(Date.parse(val.sendTime))
    },
  },
}
</script>

<style lang="scss" scoped>
.basic_info {
  width: 100%;
  height: calc(100vh - 110px);
  background: #f0f2f5;
  box-sizing: border-box;
  padding: 0 8px 8px;
  display: flex;
  flex-direction: column;

  > div {
    height: 100%;
    background: #fff;
    padding: 0 38px;

    &:last-child {
      height: 0px;
    }
  }
}

.info_operation_btn {
  width: 100%;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid #eeeeee;

  > p {
    margin: 0;
    font-size: 16px;
    font-family: PingFang SC;
    font-weight: 600;
    color: #1b94fb;
  }

  > div {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    flex: 1;

    > div {
      font-size: 12px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #666;
      margin-right: 5px;
    }

    ::v-deep .el-select {
      width: 120px;
      height: 30px;
      box-sizing: border-box;
      border-radius: 4px;

      .el-input {
        .el-input__inner {
          height: 30px;
          border-color: #e3e3e3;
          font-size: 12px;
          padding-left: 8px;
        }

        .el-input__suffix {
          height: 30px;
          display: flex;
          align-items: center;
        }
      }
    }

    input {
      width: 190px;
      height: 30px;
      background: #fff;
      border: 1px solid #e3e3e3;
      border-radius: 5px;
      font-size: 12px;
      color: #666;
      box-sizing: border-box;
      padding: 0 0.150009rem;
      margin: 0 5px 0 0.375023rem;

      &:focus {
        outline: none;
        border-color: #1890ff;
      }
    }

    ::v-deep .el-button {
      width: 60px;
      height: 30px;
      background: #1a93fa;
      border: none;
      border-radius: 5px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
}

.el-empty {
  width: 100%;
  height: calc(100% - 50px - 70px);
  box-sizing: border-box;
  overflow: auto;
}

.info_content_div {
  width: 100%;
  height: calc(100% - 50px - 70px);
  box-sizing: border-box;
  overflow: auto;

  > div {
    width: 100%;
    box-sizing: border-box;
    padding: 22px 0 10px 0;
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid #eeeeee;

    &:hover {
      background: #e3f6ff;
    }

    > div {
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      > p {
        margin: 0;
        margin-bottom: 9px;
        font-size: 16px;
        font-family: PingFang SC-Regular, PingFang SC;
        font-weight: 400;
        color: rgba(0, 0, 0, 0.65);
        line-height: 22px;
      }

      > span {
        font-size: 12px;
        font-family: PingFangSC-Regular;
        line-height: 20px;
        font-weight: normal;
        font-family: PingFangSC-Regular-, PingFangSC-Regular;
        color: rgba(0, 0, 0, 0.45);
      }

      &:nth-child(1) {
        width: 60%;
      }

      &:nth-child(2) {
        width: 20%;
      }

      &:nth-child(3) {
        width: 10%;
      }

      // &:nth-child(4) {
      //   width: 10%;
      // }
    }

    .label_div {
      width: 60px;
      height: 100%;
      justify-content: center;
      align-self: center;
      align-items: center;

      ::v-deep .el-button {
        width: 60px;
        height: 30px;
        background: #fd6c21;
        border: none;
        border-radius: 5px;
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
  }
}

.div_abstract {
  font-size: 12px;
  font-family: PingFangSC-Regular-, PingFangSC-Regular;
  font-weight: normal;
  color: rgba(0, 0, 0, 0.45);
  line-height: 20px;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}

.div_time {
  width: 120px;
  display: flex;
  align-self: flex-end;
  justify-content: flex-end;
  margin-right: 10px;

  > span {
    font-size: 12px;
    font-family: HelveticaNeue-, HelveticaNeue;
    line-height: 20px;
    color: rgba(0, 0, 0, 0.45);
    font-weight: normal;
  }
}

.table_pagination {
  width: 100%;
  height: 80px;
  box-sizing: border-box;
  padding: 8px 0;
  display: flex;

  .pagination_div {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: flex-end;

    ::v-deep .el-pagination {
      padding: 0;
      height: 30px;

      .el-pagination__sizes {
        margin: 0;

        .el-input {
          margin-right: 0;
        }
      }
    }
  }
}
</style>
